package com.lanmei.group.weixin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.lanmei.group.weixin.common.Constant;
import com.lanmei.group.weixin.model.SiteInfo;

public class SiteInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		HttpSession session = controller.getSession();
		Integer siteId = (Integer) session.getAttribute(Constant.SITE_ID);
		if(null==siteId){
			HttpServletRequest request = controller.getRequest();
			String siteUrl = request.getServerName();
			System.out.println("siteUrl:"+siteUrl);
			SiteInfo siteInfo = new SiteInfo().findSiteInfoByDomain(siteUrl);
			if(siteInfo!=null){
				System.out.println("siteInfo:"+siteInfo);
				session.setAttribute(Constant.SITE_ID, siteInfo.getSiteId());
			}
		}
		inv.invoke();
	}
}
